﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace JGSY.LHBS
{
    public partial class FrmZC : Form
    {
        public FrmZC()
        {
            InitializeComponent();
        }
        //名目文件路径
        string itemPath = CommonHelper.itemPath;
        //支出记录文件路径
        string filePath = CommonHelper.expendPath;
        int actType = 1;//提交状态  1 新增   2 修改
        ListViewItem curItem;//当前选择项
        int selIndex = -1;//选择项的索引
        int lastId = 0;//最后一条记录的编号
                       //支出列表
        List<ExpendInfo> expendList = new List<ExpendInfo>();

        private void FrmZC_Load(object sender, EventArgs e)
        {

            if (string.IsNullOrEmpty(itemPath) || string.IsNullOrEmpty(filePath))
            {
                MessageHelper.Error("支出加载", "文件路径并没有配置！");
                //禁用界面功能
                foreach (Control c in this.Controls)
                {
                    c.Enabled = false;
                }
                return;
            }
            //加载支出名目列表
            LoadCboExpendItems();
            //清空项
            listView1.Items.Clear();
            //加载已有记录列表
            LoadExpendList();
        }
        /// <summary>
        /// 加载支出名目列表
        /// </summary>
        private void LoadCboExpendItems()
        {
            comboBox1.Items.Clear();
            comboBox2.Items.Clear();
            List<string> items = CommonHelper.GetItems(2);
            if (items.Count > 0)
            {
                foreach (string item in items)
                {
                    comboBox1.Items.Add(item);
                    comboBox2.Items.Add(item);
                }
                comboBox2.Items.Insert(0, "请选择");
                comboBox2.SelectedIndex = 0;
                comboBox1.SelectedIndex = 0;
            }
        }
        /// <summary>
        /// 加载已有记录列表
        /// </summary>
        private void LoadExpendList()
        {
            List<ExpendInfo> expendData = CommonHelper.GetExpendList();
            if (expendData.Count > 0)
            {
                foreach (ExpendInfo item in expendData)
                {
                    ListViewItem li = new ListViewItem();
                    li.Text = item.ExpendId.ToString();
                    li.SubItems.Add(item.ItemName);
                    li.SubItems.Add(item.ExpendDesp);
                    li.SubItems.Add("￥" + item.Money.ToString("0.00"));
                    li.SubItems.Add(item.ExpendTime);
                    listView1.Items.Add(li);
                    expendList.Add(item);
                }
                int lastIndex = expendData.Count - 1;
                lastId = expendData[lastIndex].ExpendId;
            }
            tbNo.Text = (lastId + 1).ToString();//自动生成编号
        }

        /// <summary>
        /// add
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            //信息接收
            int id = int.Parse(tbNo.Text.Trim());
            string itemName = comboBox1.Text.Trim();
            string remark = tbRemark.Text.Trim();
            decimal money = 0.00M;
            decimal.TryParse(textBox1.Text.Trim(), out money);
            string expendTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            //信息检查
            if (string.IsNullOrEmpty(remark))
            {
                MessageHelper.Error("支出记录提交", "支出记录描述不能空！");
                tbRemark.Focus();
                return;
            }
            if (money <= 0)
            {
                MessageHelper.Error("支出记录提交", "支出金额不能小于0！");
                textBox1.Focus();
                return;
            }
            //信息封装
            ExpendInfo expendInfo = new ExpendInfo()
            {
                ExpendId = id,
                ItemName = itemName,
                ExpendDesp = remark,
                Money = money,
                ExpendTime = expendTime
            };
            //创建ListViewItem项
            ListViewItem li = CreateLvItem(expendInfo);
            //提交
            if (actType == 1)//新增
            {
                if (!File.Exists(filePath))
                {
                    TextHelper.WriteMsg("编号;名目;描述;金额;记录时间", filePath, false);
                }
                //写入支出文件
                TextHelper.WriteMsg(id + ";" + itemName + ";" + remark + ";" + money.ToString("0.00") + ";" + expendTime, filePath, true);
                //添加到lvIncomeList
                listView1.Items.Add(li);
                //添加到支出记录数据列表中
                expendList.Add(expendInfo);
                lastId = id;
            }
            else //修改
            {
                //修改后的支出信息
                listView1.Items.Insert(selIndex, li);
                //替换原来位置处的支出信息
                expendList[selIndex] = expendInfo;
                //移除原来的信息
                listView1.Items.RemoveAt(selIndex + 1);
            }
        }
        /// <summary>
        /// 根据支出信息生成列表项
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        private ListViewItem CreateLvItem(ExpendInfo info)
        {
            ListViewItem li = new ListViewItem();
            li.Text = info.ExpendId.ToString();
            li.SubItems.Add(info.ItemName);
            li.SubItems.Add(info.ExpendDesp);
            li.SubItems.Add("￥" + info.Money.ToString("0.00"));
            li.SubItems.Add(info.ExpendTime);
            return li;
        }
        private void button2_Click(object sender, EventArgs e)
        {
            tbNo.Text = (lastId + 1).ToString();
            comboBox1.SelectedIndex = 0;
            tbRemark.Text = "";
            textBox1.Text = "";
            if (actType != 1)
            {
                actType = 1;
                button1.Text = "新增";
            }
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count > 0)
            {
                var selItem = listView1.SelectedItems[0];
                actType = 2;//修改
                curItem = selItem;//当前选择项  ListVIewItem
                tbNo.Text = curItem.Text;
                comboBox1.Text = curItem.SubItems[1].Text;
                tbRemark.Text = curItem.SubItems[2].Text;
                textBox1.Text = curItem.SubItems[3].Text.Substring(1);//不显示￥在文本框
                button1.Text = "修改";
                selIndex = listView1.SelectedIndices[0];//选择项索引
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            //选择的项集合
            var selList = listView1.SelectedItems;
            if (selList.Count > 0)
            {
                DialogResult dr = MessageHelper.Confirm("删除支出记录", "你确定要删除选择支出记录信息吗？");
                if (dr == DialogResult.OK)
                {
                    List<ListViewItem> selItems = new List<ListViewItem>();
                    foreach (ListViewItem item in selList)
                    {
                        selItems.Add(item);
                    }
                    //移除选择的项
                    int i = 0;
                    while (i < selItems.Count)
                    {
                        //获取要移除的项的索引
                        int index = listView1.Items.IndexOf(selItems[i]);
                        //从列表视图中移除
                        listView1.Items.Remove(selItems[i]);
                        //从支出数据列表中移除
                        expendList.RemoveAt(index);
                        if (curItem == selItems[i])
                        {
                            //清空信息栏
                            tbNo.Text = "";
                            comboBox1.SelectedIndex = 0;
                            tbRemark.Text = "";
                            textBox1.Text = "";
                            if (actType != 1)
                            {
                                actType = 1;
                                button1.Text = "新增";
                            }
                            curItem = null;
                        }
                        i++;
                    }
                    actType = 3;//删除
                    lastId = expendList[expendList.Count - 1].ExpendId;//重新赋值最后一个编号值
                    MessageHelper.Info("删除支出记录", "选择的支出记录信息删除成功！");
                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (actType != 1 && expendList.Count > 0)
            {
                List<string> msgList = new List<string>();
                TextHelper.WriteMsg("编号;名目;描述;金额;记录时间", filePath, false);
                int id = 0;
                listView1.Items.Clear();
                foreach (ExpendInfo info in expendList)
                {
                    id += 1;
                    info.ExpendId = id;
                    string val = id + ";" + info.ItemName + ";" + info.ExpendDesp + ";" + info.Money.ToString("0.00") + ";" + info.ExpendTime;
                    msgList.Add(val);
                    listView1.Items.Add(CreateLvItem(info));
                }
                TextHelper.WriteMsgList(msgList, filePath, true);
                MessageHelper.Info("保存支出记录", "支出记录信息保存成功！");
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            //条件接收
            string kRemark = textBox3.Text.Trim();
            string fItemName = comboBox2.Text.Trim();
            if (fItemName == "请选择")
                fItemName = "";
            decimal stMoney = 0, etMoney = 0;
            if (!string.IsNullOrEmpty(textBox2.Text))
            {
                decimal.TryParse(textBox2.Text, out stMoney);
            }
            if (!string.IsNullOrEmpty(textBox4.Text))
            {
                decimal.TryParse(textBox4.Text, out etMoney);
            }
            if (etMoney > 0 && etMoney < stMoney)
            {
                MessageHelper.Error("查询支出记录", "结束范围金额不能小于开始范围金额！");
                textBox4.Focus();
                return;
            }
            IEnumerable<ExpendInfo> findList = expendList;
            //筛选工作
            if (!string.IsNullOrEmpty(kRemark))
            {
                findList = findList.Where(r => r.ExpendDesp.Contains(kRemark));
            }
            if (!string.IsNullOrEmpty(fItemName))
            {
                findList = findList.Where(r => r.ItemName == fItemName);
            }
            if (stMoney > 0)
            {
                findList = findList.Where(r => r.Money >= stMoney);
            }
            if (etMoney > 0)
            {
                findList = findList.Where(r => r.Money <= etMoney);
            }
            //查询筛选完成后的结果
            //如果有数据，才加载 
            if (findList.Count() > 0)
            {
                listView1.Items.Clear();
                foreach (ExpendInfo item in findList)
                {
                    ListViewItem li = CreateLvItem(item);
                    listView1.Items.Add(li);
                }
            }
            else
            {
                listView1.Items.Clear();
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            textBox4.Text = "";
            textBox2.Text = "";
            comboBox2.SelectedIndex = 0;
            textBox3.Text = "";
            listView1.Items.Clear();
            foreach (ExpendInfo item in expendList)
            {
                ListViewItem li = CreateLvItem(item);
                listView1.Items.Add(li);
            }
        }
    }
}
